package com.powernode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.BusinessEnum;
import com.powernode.constant.StoreConstants;
import com.powernode.domain.IndexImg;
import com.powernode.domain.Prod;
import com.powernode.ex.handler.BusinessException;
import com.powernode.feign.StoreProdFeign;
import com.powernode.mapper.IndexImgMapper;
import com.powernode.model.Result;
import com.powernode.service.IndexImgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.IndexImgServiceImpl")
public class IndexImgServiceImpl extends ServiceImpl<IndexImgMapper, IndexImg> implements IndexImgService{

    @Autowired
    private IndexImgMapper indexImgMapper;

    @Autowired
    private StoreProdFeign storeProdFeign;


    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG)
    public boolean save(IndexImg indexImg) {
        indexImg.setShopId(1L);
        indexImg.setCreateTime(new Date());
        return super.save(indexImg);
    }

    @Override
    public IndexImg queryIndexImgInfoById(Long imgId) {
        // 根据标识查询轮播图信息
        IndexImg indexImg = indexImgMapper.selectById(imgId);
        // 获取轮播图关联类型
        Integer type = indexImg.getType();
        // 判断是否有值
        if (0 == type) {
            // 说明：当前轮播图已关联商品
            // 获取关联商品的id
            Long prodId = indexImg.getProdId();
            // 远程调用：根据商品标识查询商品信息
            Result<List<Prod>> result = storeProdFeign.getProdListByIds(Arrays.asList(prodId));
            // 判断是否成功
            if (result.getCode().equals(BusinessEnum.OPERATION_FAIL.getCode())) {
                throw new BusinessException(result.getMsg());
            }
            // 获取远程调用接口的数据
            List<Prod> prodList = result.getData();
            // 获取商品对象
            Prod prod = prodList.get(0);

            indexImg.setProdName(prod.getProdName());
            indexImg.setPic(prod.getPic());
        }

        return indexImg;
    }

    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG)
    public boolean updateById(IndexImg indexImg) {
        return super.updateById(indexImg);
    }

    @Override
    @CacheEvict(key = StoreConstants.WX_INDEX_IMG)
    public boolean removeByIds(Collection<?> list) {
        return super.removeByIds(list);
    }

    @Override
    @Cacheable(key = StoreConstants.WX_INDEX_IMG)
    public List<IndexImg> queryWxIndexImgList() {
        return indexImgMapper.selectList(new LambdaQueryWrapper<IndexImg>()
                .eq(IndexImg::getStatus,1)
                .orderByDesc(IndexImg::getSeq)
        );
    }
}
